package com.niaobulashi.controller;

import com.niaobulashi.anno.SysLog;
import com.niaobulashi.entity.SysCodeEntity;
import com.niaobulashi.service.SysCodeService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;

/**
 * @program: spring-boot-12-mybatis-redis
 * @description: 数字字典
 * @author: https://niaobulashi.com
 * @create: 2019-06-30 13:47
 */
@Slf4j
@RestController
public class SysCodeController {

    @Autowired
    private SysCodeService sysCodeService;

    /**
     * 查询所有数字字典
     *
     * @return
     */
    @SysLog("查询所有数据")
    @SneakyThrows
    @RequestMapping("/getAll")
    public List<SysCodeEntity> getUser() {

        CompletableFuture<List<SysCodeEntity>> futureC = CompletableFuture.supplyAsync(() -> {
            log.info("测试异步执行");
            return sysCodeService.queryCodeAll();
        });
        List<SysCodeEntity> data = futureC.get();

        return data;
    }

    /**
     * @return
     */
    @SysLog("根据ID查询_测试缓存穿透")
    @RequestMapping("/getById")
    public SysCodeEntity getById(@RequestParam(value = "id", required = false, defaultValue = "1") Integer id) {
        SysCodeEntity data = sysCodeService.getById(id);
        return data;
    }


    /**
     * 查询所有数字字典
     *
     * @return
     */
    @RequestMapping("/getAllNoCache")
    public Object queryCodeAllNoCache() throws InterruptedException {
        List<SysCodeEntity> codeList = sysCodeService.queryCodeAllNoCache();
        return codeList;
    }


    /**
     * 查询所有数字字典
     *
     * @return
     */
    @RequestMapping("/getAllJdbc")
    public Object getAllJdbc(int i) {
        List<SysCodeEntity> codeList = sysCodeService.getAllJdbc(i);
        return codeList;
    }

    /**
     * 查询所有数字字典
     *
     * @return
     */
    @RequestMapping("/getAllNoCache2")
    public Callable<Object> queryCodeAllNoCache2() {

        Callable<Object> callable = () -> {
            List<SysCodeEntity> codeList = sysCodeService.queryCodeAllNoCache();
            return codeList;
        };

        return callable;
    }

    /**
     * 查询所有数字字典
     *
     * @return
     */
    @RequestMapping("/testLong")
    public String testLong() {
        return sysCodeService.testLong();
    }

    @GetMapping("/type")
    public String type() {
        return sysCodeService.type();
    }
    @GetMapping("/soft")
    public Object soft() {
        return sysCodeService.soft();
    }
    @GetMapping("/weak")
    public Object weak() {
        return sysCodeService.weak();
    }

    @GetMapping("/ref")
    public Object ref(int s) {
        return sysCodeService.ref(s);
    }
}
